﻿@using UserStories.Models
@using UserStories.ViewModels.Projects

@model EditViewModel

@{
    ViewBag.Title = Model.Name;
    var user = ViewBag.User as User;
}
<form action="@Url.Action("Edit")" method="POST">
    <input name="Id" type="hidden" data-bind="value: Id"/>
    @if (user != null && user.IsSysAdmin)
    {
        <text>
            Name: <input name="Name" data-bind="value: Name" />
            <br />
            Description: <input name="Description" data-bind="value: Description" />
            <br />
        </text>
    }
    else
    {
        <h2><span data-bind="text: Name"></span></h2>
        <span data-bind="text: Description"></span>
        <br/>
    }

    @if (user != null && user.IsRoleInProject(Model.Id, Role.ProductOwner))
    {
        <text>
            Releases: <input name="Releases" type="number" data-bind="value: Releases"/>
            <br/>
        </text>
    }
    
    @if (user != null && user.IsRoleInProject(Model.Id, Role.ScrumMaster))
    {
        <text>
        Sprint duration in weeks: <input name="WeeksPerSprint" type="number" data-bind="value: WeeksPerSprint"/>
        </text>
    }
    @if (user != null && user.IsSysAdmin)
    {
        <div>
            <h3>Team</h3> @Html.ActionLink("create user", "CreateUser", new { id = Model.Id })
            <table>
                <tr>
                    <th>Name</th>
                    <th>Role</th>
                    <th>Actions</th>
                </tr>
                <tbody data-bind="foreach: TeamMembers">
                    <tr>
                        <td>
                            <select data-bind="options: $root.Users,
                                                optionsText: 'Name',
                                                optionsValue: 'Id',
                                                value: Id,
                                                optionsCaption: 'Select user...',
                                                attr: { name: 'Team[' + $index() + '].Id' }" />
                        </td>
                        <td><select data-bind="options: $root.Roles,
                                            optionsText: 'Name',
                                            optionsValue: 'Id',
                                            value: RoleId,
                                            optionsCaption: 'Select role...',
                                            attr: { name: 'Team[' + $index() + '].RoleId' }" /></td>
                        <td><a href='#' data-bind='click: $parent.removeTeamMember'>Delete</a></td>
                    </tr>
                </tbody>
            </table>
            <div>
                <button data-bind='click: addTeamMember'>Add a team member</button>
            </div>
        </div>
    }
    
    @if (user != null && user.IsRoleInProject(Model.Id, Role.ProductOwner))
    {
        <div>
            <h3>User Stories</h3>
            <table>
                <tr>
                    <th>Description</th>
                    <th>Priority</th>
                    <th>Points</th>
                    <th>Actions</th>
                </tr>
                <tbody data-bind="foreach: UserStories">
                    <tr>
                        <td>
                            <input type="hidden" data-bind="value: Id, attr: { name: 'UserStories[' + $index() + '].Id' }"/>
                            <input type="text" data-bind="value: Description, attr: { name: 'UserStories[' + $index() + '].Description' }"/>
                        </td>
                        <td><input type="number" data-bind="value: Priority, attr: { name: 'UserStories[' + $index() + '].Priority' }"/></td>
                        <td><select data-bind="options: $root.PokerCards,
                                                optionsText: 'Name',
                                                optionsValue: 'Id',
                                                value: CardId,
                                                optionsCaption: 'Select card...',
                                                attr: { name: 'UserStories[' + $index() + '].CardId' }" /></td>
                        <td><a href='#' data-bind='click: $parent.removeUserStory'>Delete</a></td>
                    </tr>
                </tbody>
            </table>
            <div>
                <button data-bind='click: addUserStory'>Create user story</button>
            </div>
        </div>
    }
    
    @if (user != null && user.IsRoleInProject(Model.Id, Role.ScrumMaster))
    {
        <div>
            <h3>Sprints</h3>
            <table>
                <tr>
                    <th>Id</th>
                    <th>Release</th>
                    <th>Actions</th>
                </tr>
                <tbody data-bind="foreach: Sprints">
                    <tr>
                        <td>
                            <input type="hidden" data-bind="value: Id, attr: { name: 'Sprints[' + $index() + '].Id' }"/>
                            <span data-bind="text: $index()+1"></span>
                        </td>
                        <td><select data-bind="options: $root.R,
                                                value: ReleaseId,
                                                optionsCaption: 'Select Release...',
                                                attr: { name: 'Sprints[' + $index() + '].ReleaseId' }" /></td>
                        <td>
                            <a data-bind="visible: Id != 0, attr: {href: EditLink}">Edit</a>
                            <a href='#' data-bind='click: $parent.removeSprint'>Delete</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div>
                <button data-bind='click: addSprint'>Add Sprint</button>
            </div>
        </div>
    }
    <input type="submit" value="Save"/>
</form>


@section scripts
{ 
    <script type="text/javascript">       
        var project = $.parseJSON('@Html.Raw(Json.Encode(Model))');

        var ProjectViewModel = function(model) {
            var self = this;
            self.Id = model.Id;
            self.Name = ko.observable(model.Name);
            self.Description = ko.observable(model.Description);
            self.Releases = ko.observable(model.Releases);
            self.WeeksPerSprint = ko.observable(model.WeeksPerSprint);
            self.TeamMembers = ko.observableArray(model.Team);
            self.UserStories = ko.observableArray(model.UserStories);
            self.Sprints = ko.observableArray(model.Sprints);

            self.Roles = ko.observableArray(model.Roles);
            self.Users = ko.observableArray(model.Users);
            self.PokerCards = ko.observableArray(model.PokerCards);

            self.removeTeamMember = function(teamMember) {
                self.TeamMembers.remove(teamMember);
            };

            self.addTeamMember = function() {
                self.TeamMembers.push({ Id: 0, RoleId: 0 });
            };
            
            self.removeUserStory = function (userStory) {
                self.UserStories.remove(userStory);
            };

            self.addUserStory = function () {
                self.UserStories.push({ Id: 0, Description: "", Priority: 0, CardId: 0 });
            };

            self.R = ko.computed(function() {
                var releases = [];
                for (var i = 1; i <= self.Releases(); i++) {
                    releases.push(i);
                }

                return releases;
            });

            self.removeSprint = function (sprint) {
                self.Sprints.remove(sprint);
            };

            self.addSprint = function () {
                self.Sprints.push({ Id: 0, ReleaseId: 0, Review: "", EditLink:"" });
            };
        };

        var vm = new ProjectViewModel(project);
        ko.applyBindings(vm);
    </script>
}
